<?php
$xmlstring = '
	<ShareInfo UserName="rafale@thetigerparty.com" MediaID="" Message="Test" WebLink="http://www.amazon.com/"><facebook ID="100001690511262" token="BAAFdqWJjZCLUBAGDFFKIRgWeQvu7fuZANvdBGuZBjSmBio4LU667ctWlBhwIu2MDZAOPZCZBqQhqZBfCkk0gMNKrs1WQ16nUu9Bao59MyhOK8N4GwPPd7CcDINCHSVHB3sZD" /><twitter key="" secret="" /><instagram token="" /></ShareInfo>';

require '../Facebook/FBsdk/src/facebook.php';

if ($MediaType=='Camera') {
	if ($_FILES["file"]["error"] > 0)
	{
		switch ( $_FILES [ $field ][ 'error' ])
		{
			case 1:	// UPLOAD_ERR_INI_SIZE  
				$strError = "The file is too large (server).";  
				break ;  
			case 2:	// UPLOAD_ERR_FORM_SIZE  
				$strError = "The file is too large (form).";  
				break ;  
			case 3: // UPLOAD_ERR_PARTIAL  
				$strError = "The file was only partially uploaded.";  
				break ;  
			case 4: // UPLOAD_ERR_NO_FILE  
				$strError = "No file was uploaded.";  
				break ;  
			case 5: // missing tmp folder
				$strError = "The servers temporary folder is missing.";  
				break ;  
			case 6:	// UPLOAD_ERR_NO_TMP_DIR  
				$strError = "Failed to write to the temporary folder.";  
				break ;  
			case 7: // UPLOAD_ERR_CANT_WRITE  
				$strError = "Failed to write file to disk.";  
				break ;  
			case 8: // UPLOAD_ERR_EXTENSION
				$strError = "File upload stopped by extension.";  
				break ;
			default:
				$strError = "Unknow Error";
		}
		error_log( "[".date("Y-m-d H:i:s")."] : ".$strError."\n", 3, "upload-errors.log" );  
			
		echo '<Error Message="'.$_FILES["file"]["error"].'" Domain="BeShare" />';
		//saveError("UserUpload", NULL, date("Y-m-d H:i:s"), $_FILES["file"]["error"]);
		exit;
	}
}

$xml = simplexml_load_string($_POST['xml']);
//$xml = simplexml_load_string($xmlstring);

foreach( $xml->attributes( ) as $attr)
{
	$AttrArray[] = $attr;
}
$Title = $AttrArray[2];
$Description = $AttrArray[3];
$WebLink = $AttrArray[4];
$ImageURL = $AttrArray[5];
$MediaType = $AttrArray[6];

foreach( $xml->children() as $child)
{
//					var_dump($child);
		switch ($child->getName())
		{
			case "facebook":
				foreach( $child->attributes( ) as $attr)
					$Facebook[] = (string)$attr;
				break;
			case "twitter":
				foreach( $child->attributes( ) as $attr)
					$Twitter[] = (string)$attr;
				break;
			case "tumblr":
				foreach( $child->attributes( ) as $attr)
					$Tumblr[] = (string)$attr;
				break;
			case "instagram":
				foreach( $child->attributes( ) as $attr)
					$Instagram[] = (string)$attr;
				break;
		}
}
if(isset($Facebook) && strlen($Facebook[0]) > 0)
{
	
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => '250527955081918',
  'secret' => '8b4a436bc902681ded48cbf5eeffe760',
  'fileUpload' => true,
));
// Get User ID
$facebook->setAccessToken($Facebook[1]);
$user = $facebook->getUser();

if ($user) {
  try {
		 $attachment = array(
		 'message' => (string)$Title,
		 'image' => '@' . realpath($_FILES["file"]["tmp_name"])
		 );
	  $result = $facebook->api('/me/photos','post',$attachment);
	  
	  if ($result)
	  	echo "<Success Message=\"The requested Information was accepted\" Domain=\"BeShare\"/>";
	  else
	  	echo "<Error Message=\"The requested Information was not found\" Domain=\"BeShare\"/>";
  } 
  catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}


}

	function XMLCharEncode( $Subject )
	{
		$Serach  = array( "<", ">", "&", "'","\"" );
		$Replace = array( "&lt;","&gt;","&amp;","&apos;","&quot;");
		return str_replace($Serach, $Replace, $Subject);
	}

?>
